let userCURD = {

    user_role: sessionStorage.getItem('user_role'),
    user_id: sessionStorage.getItem('user_id'),

    // 显示项目信息
    showDataTable() {
        $('.datatable').dataTable({
            autoWidth: true,
            paging: true,
            searching: true,
            aaSorting: [], //不让其按第一列排序
            oLanguage: {
                aria: {
                    sortAscending: ': activate to sort column ascending',
                    sortDescending: ': activate to sort column descending',
                },
                sProcessing: '处理中...',
                sLengthMenu: '_MENU_ 记录/页',
                sZeroRecords: '没有匹配的记录',
                sInfo: '显示第 _START_ 至 _END_ 项记录，共 _TOTAL_ 项',
                sInfoEmpty: '显示第 0 至 0 项记录，共 0 项',
                sInfoFiltered: '(由 _MAX_ 项记录过滤)',
                sInfoPostFix: '',
                sSearch: '请输入查询信息:',
                oPaginate: {
                    sFirst: '首页',
                    sPrevious: '上页',
                    sNext: '下页',
                    sLast: '末页',
                },
            },
            // "aoColumnDefs": [{"bSearchable": false,"bVisible":false,"aTargets":[0]}],
            aoColumns: [
                {
                    mRender: function (data, type, full) {
                        sReturn = full.id
                        return sReturn
                    },
                    orderable: false,
                },
                {
                    mRender: function (data, type, full) {
                        sReturn = full.project_name
                        return sReturn
                    },
                    orderable: true,
                },
                {
                    mRender: function (data, type, full) {
                        sReturn = full.leader_name
                        return sReturn
                    },
                    orderable: false,
                },
                {
                    mRender: function (data, type, full) {
                        sReturn = full.start_time
                        return sReturn
                    },
                    orderable: false,
                },
                {
                    mRender: function (data, type, full) {
                        sReturn = full.due_time
                        return sReturn
                    },
                    orderable: true,
                },
                {
                    mRender: function (data, type, full) {
                        sReturn = full.property
                        return sReturn
                    },
                    orderable: false,
                },
                {
                    mRender: function (data, type, full) {
                        sReturn = full.progress;
                        return sReturn;
                    },
                    orderable: false,
                },
            ],
            aLengthMenu: [
                [5, 10, 15, 20, 25, 40, 50, -1],
                [5, 10, 15, 20, 25, 40, 50, '所有记录'],
            ],
            fnDrawCallback: function () {
                $('.checkboxes').uniform()
                $('.groupDao.java-checkable').uniform()
            },

            ajax: {
                async: false,
                url: '../project/action',
                type: 'POST',
                data: { user_role: this.user_role, user_id: this.user_id ,action: "get_project_info"},
                dataSrc: function (result) {
                    return result.project_list
                },
            },
        })
        $('.datatable')
            .find('.groupDao.java-checkable')
            .change(function () {
                var set = jQuery(this).attr('data-set')
                var checked = jQuery(this).is(':checked')
                jQuery(set).each(function () {
                    if (checked) {
                        $(this).attr('checked', true)
                        $(this).parents('tr').addClass('active')
                    } else {
                        $(this).attr('checked', false)
                        $(this).parents('tr').removeClass('active')
                    }
                })
                jQuery.uniform.update(set)
            })
        $('.datatable').on('change', 'tbody tr .checkboxes', function () {
            $(this).parents('tr').toggleClass('active')
        })
    },

    // 导出用户信息
    exportUserInfo() {
        let oper = 'export'
        document
            .querySelector('#datatable_user_export_button')
            .addEventListener('click', function () {
                let dataObj = {}
                let title = []
                let users = []
                var data = $('.datatable')
                    .DataTable()
                    .rows({ filter: 'applied' })
                    .data()
                for (let i = 0; i < data.length; i++) {
                    users.push(data[i])
                }
                //添加表头
                title.push('id')
                title.push('user_name')
                title.push('user_email')
                title.push('user_mobile')
                title.push('user_role')
                title.push('leaderID')

                dataObj.TableTitle = title
                dataObj.userList = users

                $.ajax({
                    async: false,
                    type: 'post',
                    url: '../userExport',
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify(dataObj),
                    success: function (response) {
                        document.getElementById('download_url').href = response.url
                    },
                    error: function () {
                        alert('服务器错误')
                    },
                })

                $('#user_export_form').modal('show')
            })
    },

    //打印用户信息
    printUserInfo() {
        document
            .getElementById('datatable_user_print_button')
            .addEventListener('click', function () {
                let userObj = {}
                let users = []
                var data = $('.datatable')
                    .DataTable()
                    .rows({ filter: 'applied' })
                    .data()
                for (let i = 0; i < data.length; i++) {
                    users.push(data[i])
                }
                userObj.userList = users
                sessionStorage.setItem('userObj', JSON.stringify(userObj))
                window.open('./printInfo.html','_blank')
            })
    },

    //统计用户信息
    staticUserInfo(){
        document
            .getElementById('datatable_user_static_button')
            .addEventListener('click', function () {
                let userObj = {}
                let users = []
                var data = $('.datatable')
                    .DataTable()
                    .rows({ filter: 'applied' })
                    .data()
                for (let i = 0; i < data.length; i++) {
                    users.push(data[i])
                }
                userObj.userList = users
                sessionStorage.setItem('userObj', JSON.stringify(userObj))
                window.open('./static.html','_blank')
            })
    },

    init() {
        // this.getUserInfo()
        this.showDataTable()
        this.exportUserInfo()
        this.printUserInfo()
        this.staticUserInfo()
    },
}

userCURD.init()
